#ifndef MYSERIALPORT_H
#define MYSERIALPORT_H

#include <QMutex>
#include <QObject>
#include <QSemaphore>
#include <QSerialPort>
#include <QThread>
#include <QTimer>
#include <QSerialPort>

#include <QDebug>
#include "global.h"

/// @brief 串口基本设置参数 与 构造函数
class MySerialPortSettings
{
public:
    MySerialPortSettings();
    MySerialPortSettings(const MySerialPortSettings &setting);
    MySerialPortSettings &operator=(const MySerialPortSettings &setting);

    QString portName;
    int baudRate;
    QSerialPort::Parity parity;
    QSerialPort::DataBits dataBits;
    QSerialPort::StopBits stopBits;
    QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl;
};

class MySerialPort : public QObject
{
    Q_OBJECT
public:
    explicit MySerialPort(const MySerialPortSettings &setting, RES &res, QObject *parent = nullptr);

    ~MySerialPort();

private:
    int init(const MySerialPortSettings &setting);

public slots:
    void slSendData(QString data);

private slots:
    void slReceiveData();
    // void slReceiveTimeOut();
    void slTimerRegularSend(bool checked);

signals:
    void sgSPReceiveData(const QByteArray data);

private:
    QThread *_thread;
    QSerialPort *_sp;
    QTimer *timerRugular;
};

#endif // MYSERIALPORT_H
